package fr.labri.buganalyzer.eclipse;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

public final class SourceFileGenetator {

	private static final String OUTPUT_SOURCES_FILE = "EclipseOne2manySourceFile.json";
	private static final String INPUT_MAPPING_FILE = "One2Many_Mapping.json";
	private static final String FOLDER  = "resources/testData/";
	private SourceFileGenetator() {
		
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException {
		List<Map<String, List<String>>> mapping = new ArrayList<>();
		
		List<Map<String, String>> urlList = new ArrayList<>();	
		
		Map<String, String> sourcesMap = null;
		
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapping = mapper.readValue(new File(FOLDER + INPUT_MAPPING_FILE), mapping.getClass());
			for (Map<String, List<String>> map : mapping) {
				for (String url : map.get("vcs")) {
					sourcesMap = new HashMap<String, String>();
					sourcesMap.put("url", url);
					sourcesMap.put("source-producer-class", "fr.labri.harmony.source.vcs.GitVCSAdapter");
					urlList.add(sourcesMap);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map <String, List<Map<String, String>>> sources = new HashMap<>();
		sources.put("sources", urlList);
		writeToJsonFile(FOLDER + OUTPUT_SOURCES_FILE, sources);
	}
	
	private static void writeToJsonFile(String file, Map <String, List<Map<String, String>>> sources) throws IOException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
		mapper.writeValue(new File(file), sources);
	}
}
